import React, { Component } from 'react';
import { Icon, Button, Table, Popconfirm, Switch, Form, Input } from 'antd';
import { IndexLink, Link } from 'react-router';

import styles from './wilddog.less';
import  EditableCell from './EditableCell';
import  AddInput from './AddInput';



export class UpList extends Component {
  constructor(props) {
    super(props);
    // console.log(props)
  }

 addOneUpHandler(name) {
    let { auth } = this.props;
    let payload = {
      createdByUserId: auth.uid,
      name: name,
      type: 'public',
      count: 0
    }
    this.props.addOneUp(payload)
  }
  updateUpCountHandler(valueObj, count) {
    valueObj = Object.assign(valueObj, { count: count });
    console.log(valueObj)
    this.props.updateUpCount(valueObj)
  }
  deleteOneUpHandler(id){
    console.log(id);
    this.props.deleteOneUp(id);
  
  }
  onChange(pagination, filters, sorter) {
    // console.log('params', pagination, filters, sorter);
  }
  render() {
      const columns = [{
      title: '活动名称',
      dataIndex: 'name',
      width: '20%',
      render: text => <span className="cellItem_name">{text}</span>,
    }, {
      title: '创建时间',
      dataIndex: 'createdAt',
      sorter: (a, b) => a.createdAt - b.createdAt,
      render: (text) => {
        let newDate = new Date();
        newDate.setTime(text);
        return <p>{newDate.toLocaleDateString("zh-cn",{weekday: "long", year: "numeric", month: "long", day: "numeric","hour": "numeric","minute":"numeric"})}</p>
      },
    }, {
      title: '点赞数',
      dataIndex: 'count',
      render: (text, record, index) => (
        <div>
         {this.props.auth.status?
        <EditableCell
          value={record.count}
          record={record}
          handUpdate={this.updateUpCountHandler.bind(this)}
        />:
        <span className="cellItem_upcount">
        {record.count}</span>
      }
      </div>)
    }, {
      title: '调用key',
      dataIndex: 'id',
    }, {
      title: '操作',
      dataIndex: '',
      key: 'x',
     render: (text, record,index) => (
        <span>
        {this.props.auth.status?
           <Popconfirm title="确认删除咩?" onConfirm={this.deleteOneUpHandler.bind(this,record.id)}>
              <a href="#">Delete</a>
            </Popconfirm>:'需登录'
            }  
          </span>
      )
    }];
    return (
     <div>
      <AddInput handAdd= {this.addOneUpHandler.bind(this)}/>
      <Table    simple  
                className={styles.table} 
                bordered 
                scroll={{x: 1200 }}            
                columns={columns} 
                dataSource={this.props.upList||[]}
                rowKey={record => record.id} 
                    />
    </div>

    )
  }
}


export default UpList
